package com.cool.commonredis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.time.Duration;

/**
 * @Author ifredomvip@gmail.com
 * @Date 2022/7/26 15:03
 * @Version 1.0.0
 * @Description
 **/
public class JedisPoolUtil {

    public static JedisPool jedisPool = null;

    public JedisPoolUtil() {
    }

    public static JedisPool getInstance() {
        if (jedisPool == null) {
            synchronized (JedisPoolUtil.class) {
                if (jedisPool == null) {
                    JedisPoolConfig config = new JedisPoolConfig();
                    config.setMaxTotal(200);
                    config.setMaxIdle(32);
                    config.setMaxWait(Duration.ofMillis(100 * 1000));
                    config.setBlockWhenExhausted(true);
                    config.setTestOnBorrow(true);

                    jedisPool = new JedisPool(config, "127.0.0.1", 6379, 60000);
                }
            }
        }
        return jedisPool;
    }

    public static void release(JedisPool jedisPool, Jedis jedis) {
        if (jedis != null) {
            jedisPool.returnResource(jedis);
        }
    }

}
